From 26c79eba733d424c6bf6a06068050b43b54705d4 Mon Sep 17 00:00:00 2001 From: robertl Date: Mon, 12 Sep 2005 16:39:44 +0000 Subject: [PATCH] Allow non-ASCII chars through shortnmae. (CET protects us.) Optionally allow repeated whitspace. Provide API for changing default name. git-svn-id: http://gpsbabel.googlecode.com/svn/trunk@1407 f51c46e8-681c-474f-0cfe-069cfd0219fb --- gpsbabel/defs.h | 2 ++ gpsbabel/mkshort.c | 64 +++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 59 insertions(+), 7 deletions(-) diff --git a/gpsbabel/defs.h b/gpsbabel/defs.h index 1b5b82a6a..70429e466 100644 --- a/gpsbabel/defs.h +++ b/gpsbabel/defs.h @@ -400,6 +400,8 @@ void setshort_goodchars(void *, const char *); void setshort_mustupper(void *, int n); void setshort_mustuniq(void *, int n); void setshort_whitespace_ok(void *, int n); +void setshort_repeating_whitespace_ok(void *, int n); +void setshort_defname(void *, const char *s); /* * Vmem flags values. diff --git a/gpsbabel/mkshort.c b/gpsbabel/mkshort.c index 1a17c9928..37a135096 100644 --- a/gpsbabel/mkshort.c +++ b/gpsbabel/mkshort.c @@ -1,7 +1,7 @@ /* Generate unique short names. - Copyright (C) 2003, 2004 Robert Lipe, robertlipe@usa.net + Copyright (C) 2003, 2004, 2005 Robert Lipe, robertlipe@usa.net This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -41,9 +41,11 @@ static const char vowels[] = "aeiouAEIOU"; typedef struct { int mustupper; int whitespaceok; + int repeating_whitespaceok; unsigned int target_len; char *badchars; char *goodchars; + char *defname; int must_uniq; queue namelist[PRIME]; int depth[PRIME]; @@ -85,6 +87,7 @@ mkshort_new_handle() h->badchars = DEFAULT_BADCHARS; h->target_len = DEFAULT_TARGET_LEN; h->must_uniq=1; + h->defname = xstrdup("WPT"); return h; } @@ -169,6 +172,9 @@ mkshort_del_handle(void *h) } } setshort_badchars(h, NULL); + if (hdr->defname) { + xfree(hdr->defname); + } xfree(hdr); } @@ -219,6 +225,10 @@ setshort_length(void *h, int l) } } +/* + * Call with L nonzero if whitespace in the generated shortname is wanted. + */ + void setshort_whitespace_ok(void *h, int l) { @@ -226,6 +236,32 @@ setshort_whitespace_ok(void *h, int l) hdl->whitespaceok = l; } +/* + * Call with L nonzero if multiple consecutive whitespace in the + * generated shortname is wanted. + */ + +void +setshort_repeating_whitespace_ok(void *h, int l) +{ + mkshort_handle *hdl = h; + hdl->repeating_whitespaceok = l; +} + +/* + * Set default name given to a waypoint if no valid is possible + * becuase it was filtered by charsets or null or whatever. + */ +void +setshort_defname(void *h, const char *s) +{ + mkshort_handle *hdl = h; + if (s == NULL) { + fatal("setshort_defname called without a valid name."); + } + hdl->defname = xstrdup(s); +} + /* * Externally callable function to set the string of characters * that must never appear in a string returned by mkshort. NULL @@ -244,6 +280,11 @@ setshort_badchars(void *h, const char *s) hdl->badchars = xstrdup(s); } } + +/* + * Only characters that appear in *s are "whitelisted" to appear + * in generated names. + */ void setshort_goodchars(void *h, const char *s) { @@ -252,6 +293,9 @@ setshort_goodchars(void *h, const char *s) hdl->goodchars = xstrdup(s); } +/* + * Call with i non-zero if generated names must be uppercase only. + */ void setshort_mustupper(void *h, int i) { @@ -259,6 +303,11 @@ setshort_mustupper(void *h, int i) hdl->mustupper = i; } + +/* + * Call with i zero if the generated names don't have to be unique. + * (By default, they are.) + */ void setshort_mustuniq(void *h, int i) { @@ -324,13 +373,12 @@ mkshort(void *h, const char *istring) } /* * Eliminate chars on the blacklist. - * Characters that aren't ASCII are never OK. */ tstring = xxstrdup(ostring, file, line); l = strlen (tstring); cp = ostring; for (i=0;ibadchars, tstring[i]) || !isascii(tstring[i])) + if (strchr(hdl->badchars, tstring[i])) continue; if (hdl->goodchars && (!strchr(hdl->goodchars, tstring[i]))) continue; @@ -343,9 +391,11 @@ mkshort(void *h, const char *istring) * Eliminate repeated whitespace. This can only shorten the string * so we do it in place. */ - for (i = 0; i < l-1; i++) { - if (ostring[i] == ' ' && ostring[i+1] == ' ') { - memmove(&ostring[i], &ostring[i+1], l-i); + if (!hdl->repeating_whitespaceok) { + for (i = 0; i < l-1; i++) { + if (ostring[i] == ' ' && ostring[i+1] == ' ') { + memmove(&ostring[i], &ostring[i+1], l-i); + } } } @@ -397,7 +447,7 @@ mkshort(void *h, const char *istring) */ if (ostring[0] == '\0') { xfree(ostring); - ostring = xstrdup("WPT"); + ostring = xstrdup(hdl->defname); } if (hdl->must_uniq) { -- 2.30.2